package dancrawler.server.events;

import dancrawler.common.Misc;
import dancrawler.server.Creature;
import dancrawler.server.GameHandler;
import dancrawler.server.Player;
import dancrawler.server.PlayerChar;
import dancrawler.server.netMsg;

/**
 *
 */
public class PlayerDeathHandler implements EventHandler {

    private Creature cr;
    private Player pl;
    private GameHandler gh;

    public PlayerDeathHandler(GameHandler gh, Creature cr) {
        this.cr = cr;
        this.gh = gh;
    }

    public void handleEvent(DeathEvent e) {
        if (e.getCreature() != null && e.getCreature() instanceof PlayerChar) {
            pl = ((PlayerChar) e.getCreature()).getPlayer();
            netMsg msg = new netMsg((short) 19, (short) 0, null);
            msg.setFinalized(true);
            pl.getCon().getOutQueue().offer(msg);
        }
        for (Player pl : gh.getPlayers()) {
            if (pl.getPc().getWorld() == e.getCreature().getWorld()) {
                netMsg msg = new netMsg((short) 10, (short) 0, null);
                byte[][] args = new byte[1][];
                args[0] = Misc.shortToBytes((short) e.getCreature().getId());
                msg.setArgs(args);
                msg.setFinalized(true);
                pl.getCon().getOutQueue().offer(msg);
            }
        }
        e.getCreature().getWorld().getTile(e.getCreature().getX(), e.getCreature().getY()).remove(e.getCreature());


    }
}
